package com.zf.minio.controller;

import com.zf.common.core.result.RestResponse;
import com.zf.minio.config.MinioClientUtils;
import com.zf.minio.vo.DownloadFile;
import com.zf.minio.vo.FileInfo;
import com.zf.minio.vo.FileStatInfo;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 文件上传下载
 */
@RestController
@RequestMapping("/file")
public class FileController {

    /**
     * 创建桶
     */
    @PostMapping("/createBucket")
    public RestResponse<?> createBucketIfAbsent(@RequestParam(name = "bucketName") String bucketName) {
        MinioClientUtils.createBucketIfAbsent(bucketName);
        return RestResponse.success();
    }

    /**
     * 列出所有的桶
     */
    @GetMapping("/listBuckets")
    public RestResponse<?> listBuckets() {
        return RestResponse.success(MinioClientUtils.listBuckets());
    }

    /**
     * 删除桶
     */
    @DeleteMapping("/deleteBucket")
    public RestResponse<?> deleteBucket(@RequestParam(name = "bucketName") String bucketName) {
        MinioClientUtils.removeBucket(bucketName);
        return RestResponse.success();
    }


    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public RestResponse<?> uploadFile(@RequestParam(name = "file") MultipartFile multipartFile) {
        FileInfo fileInfo = MinioClientUtils.uploadToDefaultBucket(multipartFile);
        return RestResponse.success(fileInfo);
    }

    /**
     * 获取文件的统计信息
     */
    @GetMapping("/fileSateInfo")
    public RestResponse<?> getFileStatInfo(@RequestParam(name = "filename") String filename) {
        FileStatInfo fileStatInfo = MinioClientUtils.getFileStatInfoFromDefaultBucket(filename);
        return RestResponse.success(fileStatInfo);
    }

    /**
     * 下载文件，传入 “endpoint/bucket/filename” 中的 filename
     */
    @GetMapping("/download")
    public void downloadFile(@RequestParam(name = "filename") String filename, HttpServletResponse response) throws IOException {
        DownloadFile downloadFile = MinioClientUtils.downloadFileFromDefaultBucket(filename, response);
        response.setContentType(downloadFile.getContentType());
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8));
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(downloadFile.getBody());
        outputStream.flush();
        outputStream.close();
    }


    /**
     * 复制文件
     * @param sourceBucket 原始文件所在的桶
     * @param sourceFile 原始文件名
     * @param targetBucket 目标桶
     * @param targetFile 复制后文件的名称
     */
    @PostMapping("/copyFile")
    public RestResponse<?> copyFile(@RequestParam(name = "sourceBucket") String sourceBucket,
                                    @RequestParam(name = "sourceFile") String sourceFile,
                                    @RequestParam(name = "targetBucket") String targetBucket,
                                    @RequestParam(name = "targetFile") String targetFile) {
        MinioClientUtils.copyFile(sourceBucket, sourceFile, targetBucket, targetFile);
        return RestResponse.success();
    }

    /**
     * 删除文件，传入 “endpoint/bucket/filename” 中的 filename
     */
    @DeleteMapping("/delete")
    public RestResponse<?> deleteFile(@RequestParam(name = "filename") String filename) {
        MinioClientUtils.deleteFromDefaultBucket(filename);
        return RestResponse.success();
    }

    /**
     * 批量删除文件，传入 “endpoint/bucket/filename” 中的 filename
     */
    @DeleteMapping("/batchDelete")
    public RestResponse<?> batchDeleteFile(@RequestBody List<String> filenameList) {
        MinioClientUtils.batchDeleteFromDefaultBucket(filenameList);
        return RestResponse.success();
    }

    /**
     * 列车桶中的所有文件
     */
    @GetMapping("/listObjects")
    public RestResponse<?> listObjects(@RequestParam(name = "bucketName") String bucketName) {
        List<FileInfo> fileInfos = MinioClientUtils.listAllFile(bucketName);
        return RestResponse.success(fileInfos);
    }
}
